package com.zx.dsptuneup_TriMic.Base;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;

import androidx.annotation.DrawableRes;
import androidx.annotation.IdRes;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import com.example.base.tools.tools.SpUtil;
import com.gyf.barlibrary.ImmersionBar;
import com.trello.rxlifecycle2.components.support.RxAppCompatActivity;
import com.zx.dsptuneup_TriMic.R;
import com.zx.dsptuneup_TriMic.Util.CommonAction;
import com.zx.dsptuneup_TriMic.widget.ui.LoadingDialog;

import butterknife.ButterKnife;


/**
 * The type Base activity.
 */
public abstract class BaseActivity extends RxAppCompatActivity {
    private static android.os.Handler handler;
    /**
     * The Sp util.
     */
    public SpUtil spUtil;
    /**
     * The Activity.
     */
    public BaseActivity activity = this;
    /**
     * The M context.
     */
    public Context mContext;
    /**
     * The M dialog.
     */
    protected LoadingDialog mDialog;

    //-----
    private WindowManager windowManager;

    /**
     *
     * 跟视野
     */

    private FrameLayout mContentContainer;
    //浮动视野
    private View mFloatView;



    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState){

        super.onCreate(savedInstanceState);
        //初始化SharedPreferencesUtil
        spUtil = new SpUtil(this, "sputil");
        initImmersionBar();
        //记录打开的activity
        mContext = this;
        createDialog();
        //把Activity添加到集合里面
        CommonAction.getInstance().addActivity(this);

//        ViewGroup mDecorView = (ViewGroup) getWindow().getDecorView();
//        mContentContainer = (FrameLayout) ((ViewGroup) mDecorView.getChildAt(0)).getChildAt(1);
//        mFloatView = LayoutInflater.from(getBaseContext()).inflate(R.layout.music_bootmview,null);

    }

    /**
     * Create dialog.
     */
    public void createDialog() {
        if (mDialog == null) {
            mDialog = new LoadingDialog(this, "loading...");
        }
    }

    /**
     * The M hide runnable.
     */
    Runnable mHideRunnable = new Runnable() {
        @Override
        public void run() {
            int flags;
            int curApiVersion = Build.VERSION.SDK_INT;
            if (curApiVersion >= Build.VERSION_CODES.KITKAT) {
                flags = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_IMMERSIVE
                        | View.SYSTEM_UI_FLAG_FULLSCREEN;

            } else {
                flags = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
            }
            getWindow().getDecorView().setSystemUiVisibility(flags);
        }
    };

    /**
     * 隐藏虚拟按键，并且设置成全屏
     */
    protected void hideBottomUIMenu() {
        if (Build.VERSION.SDK_INT > 11 && Build.VERSION.SDK_INT < 19) { // lower api
            View v = this.getWindow().getDecorView();
            v.setSystemUiVisibility(View.GONE);
        } else if (Build.VERSION.SDK_INT >= 19) {
            //for new api versions.
            View decorView = getWindow().getDecorView();
            int uiOptions = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // hide nav bar
                    | View.SYSTEM_UI_FLAG_FULLSCREEN // hide status bar
                    | View.SYSTEM_UI_FLAG_IMMERSIVE;
            decorView.setSystemUiVisibility(uiOptions);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
    }

    /**
     * 点击空白隐藏键盘
     * */
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            View v = getCurrentFocus();
            if (isShouldHideKeyboard(v, ev)) {
                hideKeyboard(v.getWindowToken());
            }
        }
        return super.dispatchTouchEvent(ev);
    }

    /**
     * 根据EditText所在坐标和用户点击的坐标相对比，
     * 来判断是否隐藏键盘，因为当用户点击EditText时则不能隐藏
     */
    private boolean isShouldHideKeyboard(View v, MotionEvent event) {
        if (v != null && (v instanceof EditText)) {
            int[] l = {0, 0};
            v.getLocationInWindow(l);
            int left = l[0],
                    top = l[1],
                    bottom = top + v.getHeight(),
                    right = left + v.getWidth();
            if (event.getX() > left && event.getX() < right
                    && event.getY() > top && event.getY() < bottom) {
                // 点击EditText的事件，忽略它。
                return false;
            } else {
                return true;
            }
        }
        // 如果焦点不是EditText则忽略，这个发生在视图刚绘制完，第一个焦点不在EditText上，和用户用轨迹球选择其他的焦点
        return false;
    }

    /**
     * 获取InputMethodManager，隐藏软键盘
     */
    private void hideKeyboard(IBinder token) {
        if (token != null) {
            InputMethodManager im = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            im.hideSoftInputFromWindow(token, InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }


//    @Override
//    protected void onPostCreate(@Nullable Bundle savedInstanceState){
//        super.onPostCreate(savedInstanceState);
//        FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
//        layoutParams.gravity = Gravity.BOTTOM;
//        mContentContainer.addView(mFloatView,layoutParams);
//    }
//
//
//    @Override
//    protected void onStart(){
//        super.onStart();
//    }
//
//    /**
//     *
//     * 重点，设置这个可以实现前进Activity时候的无动画切换
//     */
//    @Override
//    public void startActivity(Intent intent){
////        intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
//        super.startActivity(intent);
//    }
//
//    /**
//     * 重点，在这里设置按下返回键，或者返回button的时候无动画
//     */
//
//    @Override
//    public void finish(){
//        super.finish();
////        overridePendingTransition(0,0);
//    }

    /**
     * Init immersion bar.
     */
    protected void initImmersionBar() {
        ImmersionBar mImmersionBar = ImmersionBar.with(this);
        mImmersionBar.init();
    }
    @Override
    public void setContentView(int layoutResID) {
        super.setContentView(layoutResID);
        ButterKnife.bind(activity);
    }

    @Override
    protected void onDestroy(){
        super.onDestroy();
        ImmersionBar.with(this).destroy();
        Log.e("FLJ", "onDestroy->" + getClass().getSimpleName());
    }

    @Override
    protected void onPause() {
        super.onPause();
        hideDialog();
    }

    /**
     * Add fragment.
     *
     * @param mFragment       the m fragment
     * @param containerViewId the container view id
     */
    protected void addFragment(Fragment mFragment, @IdRes int containerViewId) {
        try {
            FragmentManager manager = getSupportFragmentManager();
            FragmentTransaction transaction = manager.beginTransaction();
            transaction.add(containerViewId, mFragment);
            transaction.commit();
        } catch (Exception e) {
            e.printStackTrace();
//            AfExceptionHandler.handle(e, "AfFragmentActivity Fragment 类型错误：" + mFragmentClazz);
        }
    }

    /**
     * Show dialog.
     */
    public void showDialog() {
        if (mDialog != null && !mDialog.isShowing()) {
            mDialog.show();
        }
    }

    /**
     * Hide dialog.
     */
    public void hideDialog() {
        if (mDialog != null) {
            mDialog.dismiss();
        }
    }

}

